<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>活动管理</cite></a>
    <a><cite>邀请活动</cite></a>
    <a><cite>查看活动排名</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layadmin-search">
      <form class="layui-form" method="post" lay-filter="form_invite_search" id="filterForm">
        <script type="text/html" template lay-done="layui.data.done(d.data)">
          <div class="layui-inline">
            <div class="layui-form-label">关键字搜索</div>
            <div class="layui-input-inline">
              <input type="text" name="keyword" value="{{decodeURIComponent(layui.router().search.keyword || '')}}"
                     class="layui-input"
                     placeholder="请输入昵称或手机号">
            </div>
          </div>
          <div class="layui-inline layui-btn-group">
            <button type="submit" lay-submit class="layui-btn" lay-filter="submit_invite_search">
              <i class="layui-icon layui-icon-search"></i>查询
            </button>
            <button type="submit" lay-submit class="layui-btn layui-btn-warm"
                    lay-filter="reset_invite_search">
              <i class="layui-icon layui-icon-close"></i>重置
            </button>
          </div>
          <a class="layui-btn layui-table-link"
             target="_blank"
             href="agent-admin/invites/{{layui.router().search.id}}/registers_export?token={{layui.admin.getAccessToken()}}">
            <i class="layui-icon layui-icon-add-1"></i>排行榜单导出
          </a>
        </script>
      </form>
    </div>
    <div class="layui-card-body">
      <div id="table_invite"></div>
      <script type="text/html" id="tpl_nick_name">
        {{d.user.nickname || ''}}
        {{# if (d.auth) { }}
        ({{d.auth.real_name}})
        {{# }}}
      </script>
      <script type="text/html" id="tpl_mobile">
        {{d.user.mobile || ''}}
      </script>
      <script type="text/html" id="tpl_user_count">
        {{d.realname_count}} / {{d.register_count}}
      </script>
      <script type="text/html" id="tpl_is_allowed">
        {{# if (d.is_allowed === 'enabled') { }}
        <a class="layui-table-link" lay-event="change_no">
          <i class="layui-icon layui-icon-ok"></i>
          是
        </a>
        {{# }}}
        {{# if (d.is_allowed === 'disabled') { }}
        <a class="layui-table-link layui-font-red" lay-event="change_yes">
          <i class="layui-icon layui-icon-close "></i>
          否
        </a>
        {{# }}}
      </script>
      <script type="text/html" id="tpl_tool">
<!--        <a class="layui-table-link"-->
<!--           target="_blank"-->
<!--           href="agent-admin/invites/{{d.activity_id}}/registers/{{d.id}}/invited_users_export?token={{layui.admin.getAccessToken()}}">-->
<!--          邀请客户导出-->
<!--        </a>-->

        <a class="layui-table-link"  lay-event="delivery_export">
          邀请客户导出
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.data.done = function () {
    layui.use(['fox_table'], function () {
      let $ = layui.$
        , fox_table = layui.fox_table
        , admin = layui.admin
        , form = layui.form
        , laydate = layui.laydate
        , activity_id = layui.router().search.id

      let resourceUrl = 'agent-admin/invites_ranking?id=' + activity_id

      // 列表查询
      fox_table.render({
        id: 'table_invite'
        , url: resourceUrl
        , cols: [[
          {title: '排名', field: 'ranking_num', width: 100, align: "center", sort: true}
          , {title: '手机号', templet: '#tpl_mobile', align: "center", width: 180}
          , {title: '昵称', templet: '#tpl_nick_name', align: "left", minWidth: 180}
          , {title: '注册时间', field: 'created_at', width: 180, align: "center"}
          , {title: '实名/注册', templet: '#tpl_user_count', width: 100, align: "center"}
          , {title: '参与资格', templet: '#tpl_is_allowed', width: 180, align: "center"}
          , {title: '操作', width: 180, align: 'center', fixed: 'right', toolbar: '#tpl_tool'}
        ]]
      });

      fox_table.toolbar(function (obj) {
        let data = obj.data;
        var that = this

        if (obj.event === 'change_no') {
          layer.confirm('确认取消参与资格?', function (index) {
            admin.post('agent-admin/invite/change_no', data, function (res) {
              layer.close(index);
              fox_table.reload(); //重载表格
            })
          });
        }



        if(obj.event === 'delivery_export' ){
          admin.post('agent-admin/invites/'+data.activity_id+'/registers/'+data.id+'/invited_users_export' ,obj.data,function (res) {
            layer.confirm('数据下载中，请到下载中心查看下载任务进度.', function (index) {
              layer.close(index);
              location.hash = '/ops/download';
              return false;
            });
          })
          return false;
        }


        if (obj.event === 'change_yes') {
          layer.confirm('确认恢复参与资格?', function (index) {
            admin.post('agent-admin/invite/change_yes', data, function (res) {
              layer.close(index);
              fox_table.reload(); //重载表格
            })
          });
        }
      });

      //搜索
      form.on('submit(submit_invite_search)', function (d) {
        let data = d.field
        data.id = activity_id
        fox_table.reload(data);
        return false;
      });
      //重置
      form.on('submit(reset_invite_search)', function (d) {
        $("input[name='keyword']").val('');
        $("input[name='start_date']").val('');
        $("input[name='end_date']").val('');
        fox_table.reload({id: activity_id});
        return false;
      });
    })
  }
</script>

